const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

router.get('/getindent', function(req, res, next) {
    let mysql = `SELECT * FROM order_information JOIN customer on order_information.order_username=customer.customer_id JOIN 
    pay_method on order_information.order_pay_way=pay_method.pay_id join user on order_information.order_user_id=user.user_id
    ORDER BY order_time DESC`
    db(mysql, null, function(err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询订单信息失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询订单信息成功',
            data: data
        })
    })
});
// 模糊查询订单
router.get('/getindentid',function(req,res){
    let mysql=`SELECT * FROM order_information JOIN customer on order_information.order_username=customer.customer_id JOIN 
    pay_method on order_information.order_pay_way=pay_method.pay_id join user on order_information.order_user_id=user.user_id 
    WHERE 1=1 `
    if(req.query.customer_name){
        mysql+=`AND customer_name LIKE '%${req.query.customer_name}%'`
    }
    if(req.query.order_num){
        mysql+=`AND order_num LIKE '%${req.query.order_num}%'`
    }
    if(req.query.order_time[0]){
        mysql+=`AND order_time BETWEEN '${req.query.order_time[0]}' AND '${req.query.order_time[1]}'`
    }
    if(req.query.order_state){
        mysql+=`AND order_state='${req.query.order_state}'`
    }
    if(req.query.order_type){
        mysql+=`AND order_type='${req.query.order_type}'`
    }
    mysql+=` ORDER BY order_time`
    db(mysql,null,function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '模糊查询订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '模糊查询订单成功',
            data: data
        })
    })
})

module.exports = router
